package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSavaParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏信息业务：geofenceQuery：={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //对围栏车辆数据进行处理，遍历围栏集合，依次给每一个围栏补充车辆数据
        for (int i = 0; i < list.size(); i++) {
            //1. 取出每个围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //2.封装车辆查询对象，查询条件：围栏id
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            //3.调用车辆数据层，直接根据查询条件查询当前围栏下的所有车辆
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //4.获取当前围栏绑定的车辆总数
            int totalNum = vehicleVOList.size();
            //5.定义变量保存可用车辆数
            int avaliableNum = 0;
            //6.依次遍历每一辆车，读取车辆状态，如果状态为1（空闲）可用车辆数+1
            for (VehicleVO vehicleVO : vehicleVOList) {
                if (vehicleVO.getStatus().equals("1")) {
                    avaliableNum++;
                }
            }
            geofenceVO.setAvailableNum(avaliableNum);
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSavaParam geofenceSavaParam) {
        log.debug("保存围栏信息业务：geofenceSavaParam={}", geofenceSavaParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSavaParam, geofence);
        //判断围栏id是否存在，如果存在，执行更新修改，如果不存在插入新数据
        if (geofence.getId() != null) {
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        } else {
            geofence.setStatus("1");//启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏信息业务：id={}", id);
        //业务：需要判断该围栏是否有绑定车辆，如果有绑定的车辆，就不能删除！
        //1.封装车辆查询的DTO对象
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);

        List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
        //如果该围栏下有车辆，业务层需要抛出操作失败的异常
        if (vehicleVOList.size() > 0 && vehicleVOList != null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            geofenceMapper.deleteById(id);
        }
    }
}
